using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameXNA_Master
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        RenderingData rd;
        InformationData info = new InformationData();
        SettingsData settingsdata = new SettingsData();
        BulletLogic.GameState gamestate;
        bool someToloading = false;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            //this.IsFixedTimeStep = false;                       /* Disabilito il VSync  */
            //graphics.SynchronizeWithVerticalRetrace = false;

            graphics.PreferMultiSampling = true;

            settingsdata = (new SavegameHandler()).loadSettings();

            gamestate = (new SavegameHandler()).loadGame(out someToloading);



            gameInfoState.some_To_Loading = someToloading;


            graphics.PreferredBackBufferWidth = settingsdata.widhtResolution;
            graphics.PreferredBackBufferHeight = settingsdata.heightResolution;

            graphics.IsFullScreen = settingsdata.isFullScreen;


            Content.RootDirectory = "Content";
        }

        GameLogic gameLogic;
        MenuHandler menuhandler;
        protected override void Initialize()
        {
            gameLogic = new GameLogic(this);
            menuhandler = new MenuHandler(this);
            Rendering rendering = new Rendering(this);
            this.Components.Add(rendering);
            this.Components.Add(gameLogic);
            this.Components.Add(new InputHandler(this));
            this.Components.Add(menuhandler);
            this.Components.Add(new SoundHandler(this));

            Services.AddService(typeof(InformationData), info);
            Services.AddService(typeof(SettingsData), settingsdata);

            InputService input_service = (InputService)Services.GetService(typeof(InputService));


            input_service.iPause += () =>
            {
                gameInfoState.pause = !gameInfoState.pause;
                gameLogic.Enabled = !gameInfoState.pause;
                rendering.Visible = !gameInfoState.pause;
                menuhandler.Enabled = gameInfoState.pause;
                menuhandler.Visible = gameInfoState.pause;
                Console.WriteLine("pause: " + gameInfoState.pause);

            };

            input_service.iSaveSettings += () =>
            {
                if (!(graphics.PreferredBackBufferHeight == settingsdata.heightResolution && graphics.PreferredBackBufferWidth == settingsdata.widhtResolution && graphics.IsFullScreen == settingsdata.isFullScreen))
                {
                    graphics.PreferredBackBufferHeight = settingsdata.heightResolution;
                    graphics.PreferredBackBufferWidth = settingsdata.widhtResolution;
                    graphics.IsFullScreen = settingsdata.isFullScreen;
                    graphics.ApplyChanges();
                }

            };

            input_service.iLoad += (s) =>
            {
                if (s.Equals("LOAD"))
                {
                    gamestate = (new SavegameHandler()).loadGame(out someToloading);
                    BulletLogic.projectiles_enemies_State = gamestate;
                    BulletLogic.reset_variable();
                }
                else
                    BulletLogic.reset_state(-1);

                //gameInfoState.some_To_Loading = false;
            };

            BulletLogic.useJoystick = settingsdata.useJoystic;

            input_service.iStartNewLevel += () => { gameLogic.Enabled = true; gameInfoState.Start_new_Level = false; };

            input_service.iExit += () => { this.Exit(); };

            gameLogic.Enabled = false;
            rendering.Visible = false;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            rd = (RenderingData)Services.GetService(typeof(RenderingData));

            spriteBatch = new SpriteBatch(GraphicsDevice);

        }

        float elapseTime = 0f;
        int frameCounter = 0;

        float elapsedMinute = 0;
        float elapsedSecond = 0;
        int oldLevel = 1;
        protected override void Update(GameTime gameTime)
        {

            elapseTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

            elapsedSecond += (float)gameTime.ElapsedGameTime.TotalSeconds;
            elapsedMinute += (float)gameTime.ElapsedGameTime.TotalMinutes;

            frameCounter++;

            if (elapseTime > 1)
            {
                info.fps = frameCounter;
                frameCounter = 0;
                elapseTime = 0;
            }

            info.second = (int)elapsedSecond % 60;
            info.minute = (int)elapsedMinute;




            if (oldLevel != BulletLogic.projectiles_enemies_State.LevelCounter)
                gameInfoState.Start_new_Level = true;

            gameInfoState.game_over = BulletLogic.projectiles_enemies_State.Player.Value.Life <= 0;
           

            if (gameInfoState.game_over || gameInfoState.Start_new_Level)
                gameLogic.Enabled = false;

            oldLevel = BulletLogic.projectiles_enemies_State.LevelCounter;




            base.Update(gameTime);
        }
        /*
        protected override void Draw(GameTime gameTime)
        {
            
            base.Draw(gameTime);
        }*/
    }
}
